package com.fz.us.oa.web.shiro.service;

import com.fz.us.admin.company.entity.Admin;
import com.fz.us.admin.company.service.AdminService;
import com.fz.us.admin.base.service.redis.ShardedRedisService;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Service - shiro 相关
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-28 。
 * ============================================================================
 */
@Service
public class ShiroService {

    private static final String appId1 = "b35d7751bc8ef46b87892a6abcb8f8";
    private static final String appSecret1 = "b35d7751bc8ef46b87892a6abcb8f86ede884481ef2e94aa49a7b48fffd4c13c";

    private static final String appId2 = "f86ede884481ef2e94aa49a7b48";
    private static final String appSecret2 = "b35d7751bc8ef46b87892a6abcb8f86ede884481ef2e94aa49a7b48fffd4c13c";


    private static final String APP_ID_UUID_REQUEST_TIMESTAMP = "app:$1:$2:request_timestamp";
    private static final String USER_UID_SECRET_INFO = "user:%s:secret";

    private static final int TTLDAY = 24*60*60;
    private static final int TTLQUART = 15*60;

    @Autowired
    private ShardedRedisService shardedRedisService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;

    public Admin getAdminByAccount(String account) {
        return adminService.getAdminByAccount(account);
    }

    public Admin getAdminByAdminId(String adminId) {
        return adminService.get(adminId);
    }

    public User getUserByAdminId(String adminId) {
        return userService.getUserByAdminId(adminId);
    }

    public String getUserSecretByAdminId(String adminId) {
        Admin admin = adminService.get(adminId);
        if (admin != null) {
            return admin.getSecret();
        }
        return null;
    }

    public String getAppSecretByAppId(String appId) {
        if(appId1.equals(appId)){
            return appSecret1;
        }else if(appId2.equals(appId)){
            return appSecret2;
        }
        return null;
    }

    /**
     * 同一个终端的时间戳每次访问时间不能小于上次时间
     *
     * @param appId
     * @param uuid
     * @param timestamp
     * @return
     */
    public boolean compareAndSetAppTimestamp(String appId, String uuid, String timestamp){
        String key = StringUtils.replace(APP_ID_UUID_REQUEST_TIMESTAMP,"$1",appId);
        key = StringUtils.replace(key,"$2",uuid);
        if(shardedRedisService.exists(key)){
            String lastTimestamp = shardedRedisService.get(key);
            if(StringUtils.isNotEmpty(lastTimestamp)){
                long l = Long.parseLong(lastTimestamp);
                long t = Long.parseLong(timestamp);
                if(t <= l){
                    return false;
                }
            }
        }
        shardedRedisService.set(key, timestamp);
        shardedRedisService.expire(key,TTLQUART);
        return true;
    }

}
